#ifndef WRAPPER_H
#define WRAPPER_H

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <errno.h>

#include "base.h"

int
Socket(int family, int type, int protocol)
{
    int n;
    
    if( (n = socket(family, type, protocol)) < 0)
        err_quit("socket error");
    return(n);
}

void
Bind(int fd, const struct sockaddr *sa, socklen_t salen)
{
    if(bind(fd, sa, salen) < 0)
        err_quit("bind error");
}

void
Listen(int fd, int backlog)
{
    char *ptr;
    
    if(listen(fd, backlog) < 0)
        err_quit("listen error");
}

int
Accept(int fd, struct sockaddr *sa, socklen_t *salenptr)
{
    int n;

again:
    if( (n = accept(fd, sa, salenptr)) < 0){
#ifdef EPROTO
        if(errno == EPROTO || errno == ECONNABORTED)
#else
        if(errno == ECONNABORTED)
#endif
            goto again;
        else
            err_quit("accept error");
    }
    return(n);
}

void
Write(int fd, void *ptr, size_t nbytes)
{
    if(write(fd, ptr, nbytes) != nbytes)
        err_quit("write error");
}

void
Close(int fd)
{
    if(close(fd) == -1)
        err_quit("close error");
}

#endif

